package com.stylefeng.guns.modular.biz.task;

import com.stylefeng.guns.common.persistence.dao.OrderInfoMapper;
import com.stylefeng.guns.common.persistence.dao.WorkNoticeMapper;
import com.stylefeng.guns.common.persistence.model.OrderInfo;
import com.stylefeng.guns.common.persistence.model.WorkNotice;
import com.stylefeng.guns.core.util.DateUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * 定时任务处理订单
 * @author zhouh
 * @Date 2017/10/24.
 */

@Component
public class OrderScheduled {

    private static final Logger logger = LoggerFactory.getLogger(OrderScheduled.class);

    private static final SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-DD");

    @Resource
    OrderInfoMapper orderInfoMapper;

    @Resource
    WorkNoticeMapper workNoticeMapper;

    @Scheduled(cron="0 0 4 * * ?")
    public void executeFileDownLoadTask() {
        List<OrderInfo>  list=orderInfoMapper.selectOrderNeedNotice();
        if(list.size()>0){
            WorkNotice workNotice;
            for(OrderInfo orderInfo:list){
                workNotice=new WorkNotice();
                workNotice.setWorkType(3);//滤芯更换
                workNotice.setUserName(orderInfo.getUserName());
                workNotice.setUserPhone(orderInfo.getUserPhone());
                workNotice.setAddress(orderInfo.getAddress());
                String dealTimestr=DateUtil.getAfterDayDate("7");
                try {
                    workNotice.setDealTime(sdf.parse(dealTimestr));
                } catch (ParseException e) {
                    e.printStackTrace();
                }
                workNotice.setCreateTime(new Date());
                workNotice.setOrderNo(orderInfo.getOrderNo());
                workNotice.setDeptid(orderInfo.getDeptid());
                workNoticeMapper.insert(workNotice);//出入安装工单
                Date nextTime=DateUtil.getAfterMonthDate(orderInfo.getNextNiticeTime(),orderInfo.getReplacePeriod());//下次更换周期
                try {
                    orderInfo.setNextNiticeTime(nextTime);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                orderInfoMapper.updateById(orderInfo);
            }
        }
        // 间隔2分钟,执行工单上传任务
        Thread current = Thread.currentThread();
        System.out.println("定时任务1:"+current.getId());
        logger.info("OrderScheduled 定时任务1:"+current.getId()+ ",name:"+current.getName());
    }
}
